package com.example.hotelapp.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotelapp.Adapter.DetailAdapter;
import com.example.hotelapp.Base.BaseActivity;
import com.example.hotelapp.Base.InsertBookBean;
import com.example.hotelapp.Bean.ModifyPwdBean;
import com.example.hotelapp.Bean.RoomDetailBean;
import com.example.hotelapp.R;
import com.example.hotelapp.Utils.AppNetConfig;
import com.example.hotelapp.Utils.MyCommonUtils;
import com.google.gson.Gson;
import com.kongzue.dialog.v2.WaitDialog;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static com.example.hotelapp.Utils.MyCommonUtils.CurrentTimeShow;
import static com.example.hotelapp.Utils.MyCommonUtils.ToastShow;
import static com.example.utils.CommonUtils.Delay3s;
import static com.example.utils.CommonUtils.OKPost;
import static com.example.utils.CommonUtils.SharedInfo;
import static com.example.utils.CommonUtils.setSystemBarColor;

/**
 * 预订页面
 */
public class BookActivity extends BaseActivity {
    //    找到view对象，View注入框架（添加至第三方开源框架）
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.top)
    TextView top;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.tt)
    TextView tt;
    @BindView(R.id.tv_xiugaimima)
    TextView tvXiugaimima;
    @BindView(R.id.rop2)
    RelativeLayout rop2;
    @BindView(R.id.tv_xingming)
    TextView tvXingming;
    @BindView(R.id.tv_xm)
    EditText tvXm;
    @BindView(R.id.tv_dianhua)
    TextView tvDianhua;
    @BindView(R.id.tv_sj)
    EditText tvSj;
    @BindView(R.id.tv_daodainshijian)
    TextView tvDaodainshijian;
    @BindView(R.id.tv_tijiandingdan)
    TextView tvTijiandingdan;
    private Activity activity;
    private String roomid,start,end,typename,hotelname,bedname,day,price;

    /**
     * 加载布局
     */
    @Override
    protected int setLayoutId() {
        return R.layout.activity_book;
    }

    /**
     * 初始化
     */
    @SuppressLint("SetTextI18n")
    @Override
    protected void init() {
        activity = this;
        setSystemBarColor(activity, R.color.white);
        roomid = getIntent().getStringExtra("id");
        start = getIntent().getStringExtra("start");
        end = getIntent().getStringExtra("end");
        typename = getIntent().getStringExtra("typename");
        bedname = getIntent().getStringExtra("bedname");
        day = getIntent().getStringExtra("day");
        price = getIntent().getStringExtra("price");
        hotelname = getIntent().getStringExtra("hotelname");
        tvTime.setText(start + "—" + end + "  共" + day + "晚");
        type.setText(typename);
        tt.setText(bedname);
        title.setText(hotelname);
        tvTijiandingdan.setText(price);
        System.out.println(start.substring(0,2));
        System.out.println(start.substring(3,5));
    }

    /**
     * 获取传递过来的参数
     */
    public static void start(Context context, String id, String typename, String bedname, String start, String end, String wang, String price, String hotelname) {
        Intent intent = new Intent(context, BookActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("typename", typename);
        intent.putExtra("bedname", bedname);
        intent.putExtra("start", start);
        intent.putExtra("end", end);
        intent.putExtra("day", wang);
        intent.putExtra("price", price);
        intent.putExtra("hotelname", hotelname);
        context.startActivity(intent);
    }

    /**
     * 点击事件
     */
    @OnClick(R.id.send)
    public void onViewClicked() {
        String name = tvXm.getText().toString().trim();
        String phone = tvSj.getText().toString().trim();
        if (name.isEmpty()){
            ToastShow(activity,"请填写姓名");
        }else if (phone.isEmpty()){
            ToastShow(activity,"请填写手机号");
        }else if (phone.length() != 11){
            ToastShow(activity,"请输入正确的手机号");
        }else {
            String uid = SharedInfo(activity,"GetUid","id");
            HashMap<String,String> map = new HashMap<>();
            map.put("memberId",uid);
            map.put("roomId",roomid);
            map.put("checkInDate","2021-" + start.substring(0,2) + "-" + start.substring(3,5));
            map.put("departureTime","2021-" + end.substring(0,2) +"-" +  end.substring(3,5));
            map.put("bookingRoomNum","1");
            map.put("payment",price);
            map.put("orderTime",CurrentTimeShow());
            map.put("remark","改订单已创建");
            map.put("states","待支付");
            map.put("totalPrice",tvTijiandingdan.getText().toString());
            map.put("orderDay",day);

            WaitDialog.show(activity,"支付中...");
            OKPost(map, AppNetConfig.InsertbookingList, new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            WaitDialog.dismiss();
                            ToastShow(activity,"服务器出错，请稍后再试");
                        }
                    });
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    //获取返回的json数据并解析
                    String json = response.body().string();
                    System.out.println(json);
                    InsertBookBean databean = new Gson().fromJson(json, InsertBookBean.class);
                    int code = databean.getCode();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            WaitDialog.dismiss();
                            if (code == 1) {
                                ModifyActivity();
                                ToastShow(activity,"提交成功");
                                Delay3s(activity,activity,MainActivity2.class);
                            } else {
                                ToastShow(activity,"提交失败");
                            }
                        }
                    });
                }
            });
        }
    }

    /**
     * 修改个人信息
     */
    private void ModifyActivity() {
        String id = SharedInfo(activity, "GetUid", "id");
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        map.put("memberName", tvXm.getText().toString().trim());
        OKPost(map, AppNetConfig.ModifyPwd, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

            }
        });
    }
}
